<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ProductModel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("entities/productEntity");
        $this->load->model("entities/productDetailEntity");
        $this->load->model("entities/mobileCompanyEntity");
    }

    public function getMComList()
    {
        $query = $this->db->query('SELECT a.id, a.name FROM phone_company a ORDER BY a.name ASC');
        return $query->result('MobileCompanyEntity');
    }

    public function getProductList($mComId)
    {
        $this->db->select('id, name')->where('comId =', (int)$mComId)->order_by('name', 'ASC');
        $query = $this->db->get('product');
        return $query->result('ProductEntity');
    }

    public function getMComByName($name)
    {
        $this->db->select('id, name')->where('name =', $name)->order_by('id', 'ASC');
        $query = $this->db->get('phone_company');
        return $query->result('MobileCompanyEntity');
    }

    public function getProductByName($name)
    {
        $this->db->select('id, name')->where('name =', $name)->order_by('id', 'ASC');
        $query = $this->db->get('product');
        return $query->result('ProductEntity');
    }

    public function saveMCom($name)
    {
        $this->db->trans_begin();
        $data = array('name' => $name);
        $str = $this->db->insert('phone_company', $data);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function saveProduct($name, $comId)
    {
        $this->db->trans_begin();
        $data = array('name' => $name, 'comId'=>$comId);
        $str = $this->db->insert('product', $data);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }
    
    public function saveProductDetail($data)
    {
        $aError = array();
        if (!is_numeric($data['imei'])) {
            $aError[] = "so imei chi duoc nhap la chu so";
        }
        if (!is_numeric($data['price_input'])) {
            $aError[] = "so tien chi duoc nhap la chu so";
        }
        if (count($aError) == 0) {
            $this->db->trans_begin();
            $str = $this->db->insert('product_detail', $data);
            if ($this->db->trans_status() === false) {
                $this->db->trans_rollback();
                $aError[] = "Co loi say ra khi them moi san pham !";
            } else {
                $this->db->trans_commit();
            }
        }
        return $aError;
    }
    
    public function getProductDetailByFilter($data)
    {
        //get product sole
        $query = $this->db->query('SELECT product_detail_id FROM product_sell');
        $aSell = $query->result();

        //get product detail
        $this->db->select('product_detail.*');
        $this->db->from('product_detail');
        if (count($aSell) >0) {
            $this->db->or_where_not_in('product_detail.id', $aSell);
        }
        
        if (mb_strlen(trim($data['product_id'])) > 0) {
            $this->db->where('product_detail.product_id = ', $data['product_id']);
        }
        
        $this->db->order_by('product_detail.id', 'ASC');
        $query = $this->db->get();
        return $query->result('ProductDetailEntity');
    }
}
?>